package com.zhny.iot.transparent.pump.plugin;

import com.google.common.collect.ImmutableMap;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.core.model.IRequestMessage;
import com.zhny.aiot.plugin.core.utils.MessageUtils;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.ModemDeviceFactory;
import com.zhny.aiot.plugin.modbus.device.ZhnyModemDevice;
import com.zhny.aiot.plugin.modbus.handler.model.GatewayResponseMessage;
import com.zhny.aiot.plugin.modbus.handler.model.ZhnyModemRequestMessage;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.MessageEntry;
import com.zhny.aiot.plugin.model.MessageType;
import com.zhny.aiot.plugin.model.TaskEntity;
import com.zhny.iot.transparent.pump.plugin.model.ZhnyPumpHouseControlStoreEntity;
import io.netty.buffer.ByteBuf;

import java.util.Map;

/**
 * @Author pengshuai
 * @Date 2023/11/24
 */
public class ZhnyPumpHouseControlDevice extends ZhnyModemDevice<ZhnyPumpHouseControlStoreEntity> {

    private final Map<String,Integer> CommandOperations;

    public ZhnyPumpHouseControlDevice(ModemEngine engine, IDeviceFactory<ModemConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super(engine, factory, centerFacilities, entity);
        CommandOperations = new ImmutableMap.Builder<String,Integer>()
                .put("open1",0x01)
                .put("close1",0x02)
                .put("open2",0x03)
                .put("close2",0x04)
                .put("query",0x0d)
                .build();
    }

    protected IRequestMessage getCommand(TaskEntity taskEntity) {
        String taskName = taskEntity.getMethodName();
        if(taskName.isEmpty() || !CommandOperations.containsKey(taskName)){
            return null;
        }else if(taskName.equals("query")){
            Integer operation = CommandOperations.get("query");
            return new ZhnyModemRequestMessage(operation,Integer.parseInt(getConfig().getAddress()),0x84,null);
        }else{
            Integer operation = CommandOperations.get(taskName);
            return new ZhnyModemRequestMessage(operation,Integer.parseInt(getConfig().getAddress()),0x84,null);
        }
    }

    public void onZhnyDecoder(GatewayResponseMessage message) {
        System.out.println("-------------------进入解析------------->>>>>>>>>>>>>>>");
        MessageEntry resultMsg = MessageUtils.createMessageEntry(this.getId());
        ZhnyPumpHouseControlStoreEntity storeEntity = null;
        int operation = message.getDeviceOper();
        int dataLength = message.getDataLength();
        System.out.println("dataLength : "+dataLength+" operation : "+operation+ " deviceNo : "+message.getKey());
        if(dataLength != 0x05 && dataLength != 0x0b){
            resultMsg.setType(MessageType.ERROR);
            resultMsg.setData("水泵数据返回解析异常，数据长度不匹配");
        }else{
            boolean openTag;
            if(operation == 0x01 || operation == 0x02){
                storeEntity = new ZhnyPumpHouseControlStoreEntity();
                openTag = operation == 0x01;  //true 开  false 关
                storeEntity.setAccess1(openTag);
                resultMsg.setData(storeEntity);
            }else if(operation == 0x03 || operation == 0x04){
                storeEntity = new ZhnyPumpHouseControlStoreEntity();
                openTag = operation == 0x03;  //true 开  false 关
                storeEntity.setAccess2(openTag);
                resultMsg.setData(storeEntity);
            }else if(operation == 0x0d){
                storeEntity = new ZhnyPumpHouseControlStoreEntity();
                ByteBuf data = message.getDeviceData();
                storeEntity.setPowerA (data.readUnsignedByte() == 0x01 ? "有电" : "无电");
                storeEntity.setPowerB (data.readUnsignedByte() == 0x01 ? "有电" : "无电");
                resultMsg.setData(storeEntity);
            }else{
                resultMsg.setData(null);
            }
            resultMsg.setType(MessageType.RESULT);
            save(storeEntity);
            saveReal(storeEntity);
        }
        nofity(resultMsg);
        System.out.println("-------------------完成解析------------->>>>>>>>>>>>>>>");
    }

    @Override
    protected void disposable() {

    }


}
